﻿using Studio77.GuessTheWordsWin8.Core;
using Studio77.GuessTheWordsWin8.Entities;
using Studio77.GuessTheWordsWin8.Pages;
using Studio77.GuessTheWordsWin8.Utils;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.System;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// “空白页”项模板在 http://go.microsoft.com/fwlink/?LinkId=234238 上有介绍

namespace Studio77.GuessTheWordsWin8
{
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public static MainPage Instance = null;

        public MainPage()
        {
            this.InitializeComponent();
            Instance = this;
            DisplayProperties.AutoRotationPreferences = DisplayOrientations.Landscape | DisplayOrientations.LandscapeFlipped;
        }

        /// <summary>
        /// 在此页将要在 Frame 中显示时进行调用。
        /// </summary>
        /// <param name="e">描述如何访问此页的事件数据。Parameter
        /// 属性通常用于配置页。</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            //初始化数据
            if (!StageManager.IsLoaded)
            {
                StageManager.Instance.InitAllStages();
            }

            if (!StageManager.IsLoaded)
            {
                this.bdrProgress.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
            else
            {
                this.bdrProgress.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }

            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnGift.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            else
            {
                this.btnGift.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }
        }

        public async void HideProgress()
        {
            this.bdrProgress.Visibility = Windows.UI.Xaml.Visibility.Collapsed;

            //this.btnGift.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            if (StageManager.Instance.IsDoNewUserTask)
            {
                this.btnGift.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
            }
            else
            {
                this.btnGift.Visibility = Windows.UI.Xaml.Visibility.Visible;
            }

            StageManager.Instance.LaunchCount++;
            if (StageManager.Instance.LaunchCount > 10 && !StageManager.Instance.IsCommented)
            {
                MessageDialog msg = new MessageDialog("喜欢我们的游戏吗？麻烦您去市场给我们一个五星好评吧，77Studio感谢您的支持！");
                msg.Commands.Add(new UICommand("立即去评分", async (command) =>
                {
                    Uri uri = new Uri("ms-windows-store:REVIEW?PFN=" + Package.Current.Id.FamilyName);
                    await Launcher.LaunchUriAsync(uri);
                    StageManager.Instance.IsCommented = true;
                }));
                msg.Commands.Add(new UICommand("取消", (command) =>
                {
                    StageManager.Instance.IsCommented = true;
                }));
                await msg.ShowAsync();
            }
        }

        /// <summary>
        /// 测试
        /// </summary>
        private async void LoadMap()
        {
            //载入所有地图
            MapInfo info = await WordMapLoader.LoadMap(1);
        }

        private void ButtonStartGame_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(StagePage));
        }

        private void ButtonContinueGame_Click(object sender, RoutedEventArgs e)
        {
            if (StageManager.Instance.LastStage > 0)
            {
                this.Frame.Navigate(typeof(GamePage));
            }
            else
            {
                MessageDialog md = new MessageDialog("没有保存的游戏记录");
                md.ShowAsync();
            }
        }

        private void ButtonGift_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(GiftPackage));
        }
    }
}
